package com.songshuai.webapi.crm.controller;

import com.songshuai.webapi.baseapi.dto.UserUpdatePwdDto;
import com.songshuai.webapi.baseapi.dto.feign.request.UserAddReqDto;
import com.songshuai.webapi.baseapi.dto.feign.request.UserInfoUpdateReqDto;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.pojo.BaseParmDto;
import com.songshuai.webapi.baseapi.pojo.QueryPageRes;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import com.songshuai.webapi.baseapi.dto.feign.response.AccountInfoResDto;
import com.songshuai.webapi.baseapi.dto.feign.response.AccountPageLIstResDto;
import com.songshuai.webapi.baseapi.utils.RequestParamValidUtil;
import com.songshuai.webapi.crm.constant.CrmConstant;
import com.songshuai.webapi.crm.dto.request.account.AccountPageListReqDto;
import com.songshuai.webapi.crm.dto.response.account.ConfigFunctionsResDto;
import com.songshuai.webapi.crm.dto.response.account.ConfigRolesResDto;
import com.songshuai.webapi.crm.service.AccountManagerService;
import com.songshuai.webapi.crm.service.ConfigFunctionService;
import com.songshuai.webapi.crm.service.ConfigRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 账号管理
 * @Author: lhx
 * @Date: 2018/12/20 15:08
 */
@Slf4j
@Api("账号管理")
@RestController
@RequestMapping("/api/account/")
public class AccountManagerController {

    @Value("${haha}")
    private  String config;


    @Autowired
    private AccountManagerService accountManagerService;

    @Autowired
    private ConfigRoleService configRoleService;

    @Autowired
    private ConfigFunctionService configFunctionService;


    @ApiOperation(value = "分页查询")
    @PostMapping("queryAccountListByPage")
    public QueryResponse<QueryPageRes<List<AccountPageLIstResDto>>> queryAccountListByPage(@RequestBody BaseParmDto<AccountPageListReqDto> request){
        return accountManagerService.queryAccountListByPage(request);
    }


    @Cacheable(value="queryConfigRoles")
    @ApiOperation(value = "查询角色配置")
    @GetMapping("queryConfigRoles")
    public QueryResponse<List<ConfigRolesResDto>> queryConfigRoles(){
        return QueryResponse.setResData(configRoleService.queryAllList());
    }


    @Cacheable(value="queryConfigFunctions")
    @ApiOperation(value = "查询权限列表")
    @GetMapping("queryConfigFunctions")
    public QueryResponse<List<ConfigFunctionsResDto>> queryConfigFunctions(){
        return QueryResponse.setResData(configFunctionService.queryAllList());
    }


    @ApiOperation(value = "新增用户")
    @PostMapping("addUser")
    public QueryResponse addUser(@RequestBody @Valid UserAddReqDto request, BindingResult result){
        if (result.hasErrors()){
            return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        return accountManagerService.addUser(request);
    }


    @ApiOperation(value = "根据用户id查询用户信息")
    @GetMapping("queryUserInfoById")
    public  QueryResponse<AccountInfoResDto> queryUserInfoById(@RequestParam("id")Integer id){
        return accountManagerService.queryUserInfoById(id);
    }


    @ApiOperation(value = "修改用户信息")
    @PostMapping("updateUserById")
    public QueryResponse updateUserById(@RequestBody @Valid UserInfoUpdateReqDto requset, BindingResult result){
        if (result.hasErrors()){
            return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        return accountManagerService.updateUserById(requset);
    }



    @ApiOperation(value = "禁用账号")
    @GetMapping("disableUser")
    public QueryResponse disableUser(@RequestParam("id")Integer id){
        QueryResponse queryResponse = accountManagerService.updateUserStatusById(id, CrmConstant.USER_STATUS_DISABLED);
        return queryResponse;
    }


    @ApiOperation(value = "启用账号")
    @GetMapping("enableUser")
    public QueryResponse enableUser(@RequestParam("id")Integer id){
        return accountManagerService.updateUserStatusById(id, CrmConstant.USER_STATUS_ENABLED);
    }


    @ApiOperation(value = "根据用户id修改密码")
    @PostMapping("updateUserPwdById")
    public QueryResponse updateUserPwdById(@RequestParam("id")Integer id,@RequestParam(value = "pwd",defaultValue ="songguo123",required = false)String pwd){
        return accountManagerService.updateUserPwdById(id,pwd);
    }


    @ApiOperation(value = "查询账户下跟进学员人数")
    @GetMapping("queryCustomNum")
    public QueryResponse<Integer> queryCustomNum(@RequestParam("id")Integer id){
        return QueryResponse.setResData(accountManagerService.queryCustomNum(id));
    }


    @ApiOperation(value = "删除用户")
    @GetMapping("deleteUserById")
    public QueryResponse deleteUserById(@RequestParam("id")Integer id){
        return accountManagerService.deleteUserById(id);
    }

    @ApiOperation(value = "修改密码")
    @PostMapping("updateUserPwd")
    public QueryResponse updateUserPassword(@RequestBody @Valid UserUpdatePwdDto request, BindingResult result){
        if (result.hasErrors()){
            return new QueryResponse(ResultStatus.SUCCESS.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        if(!request.getNewPwd().equals(request.getConfirmPwd())){
            return new QueryResponse(ResultStatus.ERROR_UPDATE_FAILED.getErrorCode(),"两次密码输入不一致");
        }
        return accountManagerService.updateUserPassword(request);
    }
    @ApiOperation(value = "测试专用")
    @GetMapping("togetSomePort")
    public QueryResponse updateUserPassword(){

        return QueryResponse.setResData(config);
    }


















}
